import * as TYPES from '../actions/actionTypes';
import o from '../components/o';
import Toast from 'react-native-root-toast'
export function loadMore(api, params) {
    return (dispatch, getState) => {
        const { dataList, hasMore, loading } = getState().Sale;
        const { pageNum, keyword, pageSize, filter, expand, orderByDesc } = params;
        const skip = (pageNum - 1) * pageSize
        dispatch({ type: TYPES.SALE_DATA_ING });
        const { token } = getState().User;
        const _api = api + "?token=" + token;
        if (!filter) {
            o(_api).take(pageSize).skip(skip).expand(expand).inlineCount().orderByDesc(orderByDesc).get().then(oHandler => {
                dispatch(loadMoreSuccess(oHandler, params, dataList))
            }).fail((e) => {
                dispatch(loadMoreFail())
            })
        } else {
            o(_api).take(pageSize).skip(skip).filter(filter).inlineCount().get().then(oHandler => {
                dispatch(loadMoreSuccess(oHandler, params, dataList))
            }).fail((e) => {
                dispatch(getDataFail(TYPES.SALE_DATA_DETAIL_FAIL))
            })
        }
    }
}

export function loadMoreSuccess(oHandler, params, dataList) {
    const { data, inlinecount } = oHandler;
    const { pageNum, pageSize, keyword } = params;
    let preData = dataList ? dataList : [];
    for (let i = 0; i < data.length; i++) {
        //计算单项plist中pnumber总和  进库的count 比较得出入库状态
        const saleList = data[i].sale_salelist; //销售项
        let outs = 0;//已出库数
        const species = saleList ? saleList.length : 0;//商品种类
        let SNumber = 0 //总采购数
        for (let j = 0; j < saleList.length; j++) {
            const SNumber_j = saleList[j].SNumber;
            const out_j = saleList[j].salelist_out.length;
            SNumber = SNumber + SNumber_j;
            outs = outs + out_j;
        }

        const diff = SNumber - outs;
        let state = 1;
        if (diff == 0) {
            state = 1 //已出库
        } else if (diff < SNumber && diff > 0) {
            state = 2//部分出库
        } else {
            state = 3//待出库
        }
        data[i].species = species;
        data[i].SNumber = SNumber;
        data[i].state = state;
        data[i].outs = outs;
        //图片处理
        const { erp_sale_file } = data[i]
        if (erp_sale_file) {
            let { Url } = erp_sale_file;
            if (Url && Url.length > 0) {
                data[i].images = Url.split(',')
            }
        }
    }
    if (pageNum * pageSize < inlinecount) {
        return { type: TYPES.SALE_DATA_SUCCESS, dataList: preData.concat(data), pageNum: pageNum, hasMore: true, keyword: keyword }
    } else {
        return { type: TYPES.SALE_DATA_SUCCESS, dataList: preData.concat(data), pageNum: pageNum, hasMore: false, keyword: keyword }
    }
}
export function getDataFail(type) {
    Toast.show("获取数据异常", {
        duration: Toast.durations.LONG,
        position: Toast.positions.CENTER,
    })
    return { type: type }
}

export function initialDetail(item) {
    return (dispatch) => {
        dispatch({ type: TYPES.SALE_DETAIL_DATA_INIT, data: item })
    }
}

export function getDetail(params) {
    return (dispatch, getState) => {
        dispatch({ type: TYPES.SALE_DATA_DETAIL_ING });
        const { api, filter, select, expand } = params;
        const { token } = getState().User;
        const _api = api + "?token=" + token;
        o(_api).filter(filter).select(select).expand(expand).get().then(oHandler => {
            let saleList = oHandler.data
            //计算每一个商品的待出库数  商品相关属性
            for (let i = 0; i < saleList.length; i++) {
                let goods = saleList[i].salelist_archives;
                saleList[i].Name = goods.Name;
                saleList[i].image = [];
                saleList[i].Code = goods.Code;
                saleList[i].outs = saleList[i].salelist_out.length;
                const file = goods.archives_file;
                if (file) {
                    saleList[i].images = file.Url.split(',')
                }
            }
            console.log(saleList)
            dispatch({ type: TYPES.SALE_DATA_DETAIL_SUCCESS, data: saleList })
        }).fail(e => {
            dispatch(getDataFail(TYPES.SALE_DATA_DETAIL_FAIL))
        })
    }
}

//出库操作成功 返回list页面 更新list中出库状态
export function updateSaleList(params) {
    return (dispatch, getState) => {
        const { codesArr, SaleId } = params;
        let dataList_old = getState().Sale.dataList;
        let dataList = [].concat(dataList_old)
        for (var i = 0; i < dataList.length; i++) {
            for (var i = 0; i < dataList.length; i++) {
                if (dataList[i].SaleId == SaleId) {
                    const { SNumber, outs } = dataList[i];
                    const newouts = outs + codesArr.length;
                    const diff = SNumber - newouts;
                    let state = 1;
                    if (diff == 0) {
                        state = 1 //已出库
                    } else if (diff < SNumber && diff > 0) {
                        state = 2//部分出库
                    } else {
                        state = 3//待出库
                    }
                    dataList[i].outs = newouts;
                    dataList[i].state = state;
                }
            }
        }
        dispatch({ type: TYPES.SALE_DATA_UPDATE, data: dataList })
    }
}

